package com.bingqing.service;

import com.bingqing.dao.TypeDao;
import com.bingqing.entity.Admin;
import com.bingqing.entity.Params;
import com.bingqing.entity.Type;
import com.bingqing.exception.CustomException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * ClassName: TypeService
 * Package: com.bingqing.service
 * Description:
 *
 * @Author liuyujie
 * @Create 2024-03-10 9:23
 * @Version 1.0
 */
@Service
public class TypeService {
    @Resource
    TypeDao typeDao;


    //分页查询
    public PageInfo<Type> findBySearch(Params params) {
        PageHelper.startPage(params.getPageNum(),params.getPageSize());
        List<Type> list=  typeDao.findBySearch(params);
        return PageInfo.of(list);
    }



    public void add(Type type) {
        //1.判断是否为空
        if(type.getName() == null || "".equals(type.getName())){
            throw new CustomException("请输入名字");
        }
        //2.判断是否重复
        Type dbType = typeDao.selectByName(type.getName());
        if(dbType!=null){
            throw new CustomException("该类别已存在，请重新输入");
        }
        //3.插入数据
        typeDao.insertSelective(type);
    }

    public void update(Type type) {
        //1.判断是否为空
        if(type.getName() == null || "".equals(type.getName())){
            throw new CustomException("请输入名字");
        }
        ////2.判断是否重复
        //Type dbType = typeDao.selectByName(type.getName());
        //if(dbType!=null){
        //    throw new CustomException("该类别已存在，请重新输入");
        //}
        typeDao.updateByPrimaryKeySelective(type);
    }

    public void deleteById(Integer id) {
        typeDao.deleteByPrimaryKey(id);
    }

    public List<Type> findAll() {
        List<Type> list = typeDao.findAll();
        return list;
    }
}
